#include <QFileInfo>
#include <QDir>
#include <QDebug>
#include <QScrollBar>

#include "imagegallery.h"
#include "imagegalleryitem.h"
#include "flowlayout.h"

namespace Jcr
{
class ImageGalleryPrivate
{
public:
    ImageGalleryPrivate(ImageGallery* q);
    void layoutItems();

    QWidget* widget = nullptr;
    FlowLayout* layout = nullptr;
    ImageGallery* q_ptr;
    QString dir;
    QList<Jcr::ImageGalleryItem*> items;
};

ImageGalleryPrivate::ImageGalleryPrivate(ImageGallery *q) : q_ptr(q)
{
    widget = new QWidget(q);
    layout = new FlowLayout(q);
    widget->setLayout(layout);
    q->setWidget(widget);
    q->setWidgetResizable(true);
}

void ImageGalleryPrivate::layoutItems()
{

}

ImageGallery::ImageGallery(QWidget *parent) : QScrollArea(parent)
{
    d_ptr = new ImageGalleryPrivate(this);
}

ImageGallery::~ImageGallery()
{
    clear();
    delete d_ptr;
}

void ImageGallery::load(const QString& dir)
{
    if (d_ptr->dir != dir) {
        qDebug() << "loading : " << dir;
        clear();
        QDir folder(dir);
        QFileInfoList files = folder.entryInfoList(QDir::Files | QDir::NoDotAndDotDot);
        foreach (auto file, files) {
            Jcr::ImageGalleryItem* igi = new Jcr::ImageGalleryItem(file.absoluteFilePath());
            d_ptr->items.append(igi);
            d_ptr->layout->addWidget(igi);
        }
        d_ptr->layoutItems();
        scrollToTop();
    }
}

void ImageGallery::addImage(const QString &file)
{
    Jcr::ImageGalleryItem* item = new Jcr::ImageGalleryItem();
    item->setFile(file);
    d_ptr->layout->addWidget(item);
}

void ImageGallery::clear()
{
    qDeleteAll(d_ptr->items);
    d_ptr->items.clear();
    d_ptr->layout->clear();
    d_ptr->layoutItems();
}

void ImageGallery::view()
{
    foreach (auto item, d_ptr->items)
        item->setState(Jcr::ImageGalleryItem::View);
}

void ImageGallery::edit()
{
    foreach (auto item, d_ptr->items)
        item->setState(Jcr::ImageGalleryItem::Edit);
}

void ImageGallery::selectAll(bool select)
{
    foreach (auto item, d_ptr->items) {
        item->setState(Jcr::ImageGalleryItem::Select);
        item->select(select);
    }
}

void ImageGallery::setAsDetailed()
{
    QString detailedName;
    QString msg;
    int detailedImageCounter = 0;
    foreach (auto item, d_ptr->items) {
        if (item->selected()) {
            ++detailedImageCounter;
            detailedName = QString("%1_d").arg(item->baseName());
            item->rename(detailedName);
        }
    }

    msg = tr("Set %1 images to detailed.").arg(detailedImageCounter);
    emit notify(msg);
}

void ImageGallery::setAsSmall()
{
    QString detailedName;
    QString msg;
    int smallImageCounter = 0;
    foreach (auto item, d_ptr->items) {
        if (item->selected()) {
            ++smallImageCounter;
            detailedName = QString("%1_s").arg(item->baseName());
            item->rename(detailedName);
        }
    }
    msg = tr("Set %1 images to small.").arg(smallImageCounter);
    emit notify(msg);
}

void ImageGallery::setAsOutdoor()
{
    QString detailedName;
    QString msg;
    int outdoorImageCounter = 0;
    foreach (auto item, d_ptr->items) {
        if (item->selected()) {
            ++outdoorImageCounter;
            detailedName = QString("%1_o").arg(item->baseName());
            item->rename(detailedName);
        }
    }
    msg = tr("Set %1 images to outdoor.").arg(outdoorImageCounter);
    emit notify(msg);
}

void ImageGallery::scrollToEnd()
{
    verticalScrollBar()->setValue(verticalScrollBar()->maximum());
}

void ImageGallery::scrollToTop()
{
    verticalScrollBar()->setValue(0);
}

} // namespace
